package com.example.big_event.controller;

import com.example.big_event.Utils.Result;
import com.example.big_event.Utils.ThreadLocalUtil;
import com.example.big_event.pojo.entity.Category;
import com.example.big_event.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 添加新的分类
     * @param categoryName 分类名称
     * @param categoryAlias 分类别名
     * @return 成功或者失败的结果
     */
    @PostMapping("/add")
    public Result<Category> addCategory
    (@RequestParam String categoryName,@RequestParam String categoryAlias){
        // 从线程中获取用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer createId = (Integer) map.get("id");
        System.out.println(createId);
        //  将分类名称和分类别名同步到数据库中
       boolean isSuccess = categoryService.save(new Category(categoryName,categoryAlias,createId));

        return isSuccess?Result.success():Result.error("添加失败");
    }

    /**
     * 查询所有分类的数据
     * @return 成功或者失败的结果
     */
    @GetMapping("/queryAll")
    public Result<List<Category>> queryAll(){
        List<Category> list = categoryService.list();
        return  Result.success(list);
    }

    /**
     * 查询分类数据的详细信息
     * @param id 需要查询的分类数据的id
     * @return 成功或者失败的结果
     */
    @GetMapping("/detail")
    public Result<Category> getCategoryById(@RequestParam Integer id){
       Category category =  categoryService.getById(id);
        return Result.success(category);
    }


    @PatchMapping("/update")
   public Result<Category> updateCategory(@RequestBody Category category){
       boolean isSuccess =  categoryService.updateById(category);
        return  isSuccess? Result.success():Result.error("更新失败");
    }
}
